﻿#include "SetThresholdDialog.h"
#include "ui_SetThresholdDialog.h"

#include "DataInfo.h"
#include "CommonFunc.h"

SetThresholdDialog::SetThresholdDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SetThresholdDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setAttribute(Qt::WA_TranslucentBackground);

    ui->pushButton_change->setEnableLoading(true);
    QObject::connect(ui->pushButton_change, &LoadingButton::clicked, this, [&]{
        ui->pushButton_cancel->setEnabled(false);
        ui->spinBox->setEnabled(false);
    });

    appendTypeToFunc(XSetThreshold_Result, [&]{
        ui->pushButton_cancel->setEnabled(true);
        ui->spinBox->setEnabled(true);
        ui->pushButton_change->stopLoading();
        if (g_httpReplayResult.first != 0) {
            ui->messageLabel->setText(g_httpReplayResult.second);
        } else {
            g_iThreshold = ui->spinBox->value();
            this->accept();
        }
    });
}

SetThresholdDialog::~SetThresholdDialog()
{
    delete ui;
    removeTypeToFunc(XSetThreshold_Result);
}

void SetThresholdDialog::setThresholdValue(int value)
{
    ui->spinBox->setValue(value);
}

void SetThresholdDialog::on_pushButton_cancel_clicked()
{
    this->reject();
}

void SetThresholdDialog::on_pushButton_change_clicked()
{
    int threshold = ui->spinBox->value();
    sendChangeThresholdValue(threshold);
}
